package no.ntnu.fp.model;

import java.sql.ResultSet;
import java.util.ArrayList;

import no.ntnu.fp.storage.DBConnection;

public class Meeting extends ActiveRecord {
	private String place;
	private String date;
	private String start;
	private String end;
	private int numberOfParticipants;
	private String description;
	private String meetingLeader;
	private ResultSet rs;
	private boolean hasBookedRoom;
	private ArrayList<String> usersInvited;
	
	public Meeting(ArrayList<String> usersInvited, String leader, String date, String start, String end, String place, String description, boolean hasBookedRoom) {
		this.meetingLeader = leader;
		this.usersInvited = usersInvited;
		this.date = date;
		this.start = start;
		this.end = end;
		this.place = place;
		this.description = description;
		this.hasBookedRoom = hasBookedRoom;
	}
	
	private void updateOtherTables() {
		// OPPDATERER MOTEINNKALLING, HOLDESI
		db = new DBConnection();
		
		String queryAvtaleID = "select AvtaleID " +
			"from Avtale " +
			"where Sted = " + "\""+ place +"\"" + " " +
			"and Dato = " + "\""+ date +"\"" + " " +
			"and Starttidspunkt = " + "\""+ start +"\"" + " " +
			"and Sluttidspunkt = " + "\""+ end +"\"" + " " +
			"and AntallDeltakere = " + numberOfParticipants + " " +
			"and Beskrivelse = " + "\""+ description +"\"";
		
		rs = db.makeSingleQuery(queryAvtaleID);
		
		int avtaleID = -1;
		if (next(this.rs)) {
			avtaleID = getInt(rs, "AvtaleID"); //g�r ut fra at det ikke er duplikater i avtale-tabellen
		} else
			System.out.println("ingen resultater");
	
		if (hasBookedRoom) {
			String queryMoteromID = "select MoteromID " +
					"from Moterom " +
					"where navn = " + "\"" + place + "\"";
			rs = db.makeSingleQuery(queryMoteromID);
			int moteromID = -1;
			if (next(rs)) {
				moteromID = getInt(rs, "MoteromID"); //g�r ut fra at det ikke er duplikater
			}
	
			String query = "insert ignore into HoldesI(AvtaleID, MoteromID) values (?,?)";
			setPreparedStatement(query);
			setInt(1, avtaleID);
			setInt(2, moteromID);
			addExecuteAndClosePS();
		}
		//oppdaterer Moteinnkalling
		for (int i = 0; i < usersInvited.size(); i++) {
			String query = "insert ignore into Moteinnkalling(AvtaleID, status, Deltaker) values (?,?,?)";
			setPreparedStatement(query);
			setInt(1, avtaleID);
			setString(2, "Avventer");
			setString(3, usersInvited.get(i));
			addExecuteAndClosePS();
		}
		//oppdaterer oprettermote
		String query = "insert ignore into OppretterMote(Moteleder, AvtaleID) values (?,?)";
		setPreparedStatement(query);
		setString(1, meetingLeader);
		setInt(2, avtaleID);
		addExecuteAndClosePS();
		closeDBConnection();
	}
	
	@Override
	public void save() {
		db = new DBConnection();
		
		String query="insert ignore into Avtale(sted,dato,starttidspunkt,sluttidspunkt,antalldeltakere," +
				"beskrivelse) values (?,?,?,?,?,?)";
		setPreparedStatement(query);
		
		setString(1, this.place);
		setString(2, this.date);
		setString(3, this.start);
		setString(4, this.end);
		setInt(5, this.numberOfParticipants);
		setString(6, this.description);
		addExecuteAndClosePS();
		closeDBConnection();
		
		updateOtherTables();
	}

	@Override
	public void edit(int index) {
	}

	@Override
	public void delete() {
	}
	public String toString(){
		return "Dato: " + date + ", Starttidspunkt: " + start + ", Sluttidspunkt: " + end + ", Beskrivelse: " + description;
	}
	
}
